//
//  swizzle.swift
//  live
//
//  Created by melvyn on 2018/8/16.
//  Copyright © 2018 NEET. All rights reserved.
//

import Foundation
import ObjectiveC.runtime

func swizzle(_ aClass: AnyClass, selector originalSelector: Selector, with swizzledSelector: Selector) {
    
    let originalMethod = class_getInstanceMethod(aClass, originalSelector)!
    let swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector)!
    
    let success = class_addMethod(aClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
    
    if success {
        class_replaceMethod(aClass, swizzledSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

protocol MethodSwizzling {
    static func swizzling()
}
